package com.example.superoa.controller;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.example.superoa.common.Shijian;
import com.example.superoa.model.mappers.RecessMapper;
import com.example.superoa.model.pojos.Staff;
import com.example.superoa.model.service.AttendMonthService;
import com.example.superoa.model.service.BusinessService;
import com.example.superoa.model.service.DemoService;
import com.example.superoa.model.service.LeaveService;
import com.example.superoa.model.service.OvertimeService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;

@Controller
public class AttendMonthController {
	@Autowired
	private AttendMonthService ser;
	
	@Autowired
	private RecessMapper r_map;
	
	@Autowired
	private DemoService d_biz;
	
	
	Shijian sj=new Shijian();
	@RequestMapping("month_selectAll")
	@ResponseBody
	public Map<String,Object> LeaveSelectAll(String date,Integer staff_id,int page,int rows) {
		Map<String,Object> map = new HashMap<>();
		//先分页
		Page<Object> pg = PageHelper.startPage(page, rows);
		Date time=null;
		try {
			if(!date.equals("")) {
				time=new SimpleDateFormat("yyyy-MM-dd").parse(date);
			}else {
				time=new Date(sj.getNetworkTime());
			}
		} catch (Exception e) {
			// TODO: handle exception
		}
		System.out.println(time);
		List<Integer> list=sj.getyearmonth(time);
		JSONArray json = ser.selectShi(list.get(0), list.get(1),staff_id);
		int i=1;
		for (Object object : json) {
			JSONObject ob=(JSONObject)object;
			ob.put("sortable", i);
			ob.put("position_name", ser.select(ob.getInteger("staff_id")));
			i++;
		}
		//总记录
		map.put("total", pg.getTotal());
		//页内容
		map.put("rows", json);
		return map;
	}
	@RequestMapping("selectMonth")
	@ResponseBody
	public JSONArray selectMonth(){
		JSONArray arr=new JSONArray();
		int i=1;
		for (Date o : ser.selectMonth()) {
			JSONObject obj=new JSONObject();
			List<Integer> list=sj.getyearmonth(o);
			obj.put("month", list.get(1));
			obj.put("year", list.get(0));
			obj.put("id", i);
			arr.add(obj);
			i++;
		}
		return arr;
		
	}
	@RequestMapping("kq-attendance-month-xiang")
	public void selelctXiang(int id,Date date,HttpSession session) {
		session.setAttribute("xid", id);
		session.setAttribute("xdate", date);
		session.setAttribute("pojos", d_biz.selectID(id));
		
	}
	@RequestMapping("xiang")
	@ResponseBody
	public JSONArray selectXiang(HttpSession session) {
		Integer id=(Integer) session.getAttribute("xid");
		Date date=(Date) session.getAttribute("xdate");
		//法定假
		JSONArray one=r_map.selectjiaTow();
		//公司假
		JSONArray tow=r_map.selectjiathree();
		List<Integer> list=sj.getyearmonth(date);
		JSONObject staff=ser.selectShi(list.get(0), list.get(1), id).getJSONObject(0);
		return ser.selectMonthXiang(id,staff.getDate("Dateentry"), date, one, tow);
	}
}
